package com.config.sharding.stock.circulation.detail.hint;

import com.config.ShardingDictionaries;
import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.config
 * @date:2021/9/15
 */
@Component
public class StockDetailTableHint extends ShardingDictionaries implements HintShardingAlgorithm<String> {
    @Override
    public Collection<String> doSharding(Collection<String> collection, HintShardingValue<String> hintShardingValue) {
        String a = (String) hintShardingValue.getValues().toArray()[0];
        String b = (String) hintShardingValue.getValues().toArray()[1];
        String c = (String) hintShardingValue.getValues().toArray()[2];

        String a1 = null;
        String b1 = null;
        String c1 = null;
        Collection<String> list = new ArrayList<>();
        if (a.indexOf("first") != -1) {
            a1 = a;
        } else if (a.indexOf("second") != -1) {
            b1 = a;
        } else {
            c1 = a;
        }
        if (b.indexOf("first") != -1) {
            a1 = b;
        } else if (b.indexOf("second") != -1) {
            b1 = b;
        } else {
            c1 = b;
        }
        if (c.indexOf("first") != -1) {
            a1 = c;
        } else if (c.indexOf("second") != -1) {
            b1 = c;
        } else {
            c1 = c;
        }
        a1 = super.key(a1.substring(a1.indexOf("_")+1));
        b1 = super.key(b1.substring(b1.indexOf("_")+1));
        c1 = super.detailKey(c1.substring(c1.indexOf("_")+1));

        String table = hintShardingValue.getLogicTableName() + "_" + a1 + "_" + b1 + "_" + c1;
        list.add(table);
        return list;
    }
}
